/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "makecard_srv_client.h"
#include "string_ex.h"
#include "datetime_ex.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "if_system_ability_manager.h"
#include "system_ability_definition.h"
#include "makecard_errors.h"
#include "makecard_common.h"
#include "hilog_wrapper.h"

namespace SwanLink {
namespace Finance {
MakecardSrvClient::MakecardSrvClient() {}
MakecardSrvClient::~MakecardSrvClient() {}

ErrCode MakecardSrvClient::Connect()
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (proxy_ != nullptr) {
        return ERR_OK;
    }
    sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    if (sysMgr == nullptr) {
        MAKECARD_HILOGE(MAKECARD_SRV, "failed to GetSystemAbilityManager,%{public}d", FINANCE_MAKECARD_SERVICE_ID);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }
    sptr<IRemoteObject> remoteObject_ = sysMgr->CheckSystemAbility(FINANCE_MAKECARD_SERVICE_ID);
    if (remoteObject_ == nullptr) {
        MAKECARD_HILOGE(MAKECARD_SRV, "failed to GetSystemAbility by %{public}d", FINANCE_MAKECARD_SERVICE_ID);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }

    deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new MakecardSrvDeathRecipient());
    if (deathRecipient_ == nullptr) {
        MAKECARD_HILOGE(MAKECARD_SRV, "failed to Create DeathRecipinent,%{public}d", FINANCE_MAKECARD_SERVICE_ID);
        return ERR_NO_MEMORY;
    }
    if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
        MAKECARD_HILOGW(MAKECARD_SRV, "Add death recipient to failed,%{public}d", FINANCE_MAKECARD_SERVICE_ID);
        return E_ADD_DEATH_RECIPIENT_FAILED;
    }

    proxy_ = iface_cast<IMakecardSrv>(remoteObject_);
    return ERR_OK;
}

void MakecardSrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
{
    std::lock_guard<std::mutex> lock(mutex_);
    RETURN_IF(proxy_ == nullptr);
    auto serviceRemote = proxy_->AsObject();
    if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
        serviceRemote->RemoveDeathRecipient(deathRecipient_);
        proxy_ = nullptr;
    }
}

void MakecardSrvClient::MakecardSrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
{
    if (remote == nullptr) {
        return;
    }
    MakecardSrvClient::GetInstance().ResetProxy(remote);
}

int MakecardSrvClient::SetConfig(int handle, int configType, int configValue)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_JS_NAPI, "SetConfig failed");
        return -1;
    }
    return proxy_->SetConfig(handle, configType, configValue);
}

int MakecardSrvClient::SetStandbyParam(int handle, int dir, int pos, int standbyTime)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_JS_NAPI, "SetStandbyParam failed");
        return -1;
    }
    return proxy_->SetStandbyParam(handle, dir, pos, standbyTime);
}

int MakecardSrvClient::PrintCard(int handle, BmpInfo cardBmpInfo)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_JS_NAPI, "PrintCard failed");
        return -1;
    }
    return proxy_->PrintCard(handle, cardBmpInfo);
}

int MakecardSrvClient::GetDeviceInfo(int handle, int infoType, char *infoValue)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_JS_NAPI, "GetDeviceInfo failed");
        return -1;
    }
    return proxy_->GetDeviceInfo(handle, infoType, infoValue);
}

int MakecardSrvClient::GetErrorString(int handle, int status, char* statusDesc)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_JS_NAPI, "GetErrorString failed");
        return -1;
    }
    return proxy_->GetErrorString(handle, status, statusDesc);
}

int MakecardSrvClient::MoveCard(int handle, int postionMoveTo)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_JS_NAPI, "MoveCard failed");
        return -1;
    }
    return proxy_->MoveCard(handle, postionMoveTo);
}

int MakecardSrvClient::FlipCard(int handle)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_JS_NAPI, "FlipCard failed");
        return -1;
    }
    return proxy_->FlipCard(handle);
}



}  // namespace Finance
}  // namespace SwanLink
